home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-07-31 | 4.9 KB | 167 lines | [TEXT/MMCC] |
- /**************************************************************************
- UGBDraw
-
- Public domain, by Zig Zichterman.
-
- Utility functions for drawing in color. All these functions assume
- color quickdraw.
-
- This class implements 3D drawing according to the guidelines
- suggested in _develop_ 15. Some of the drawing code is taken from
- the public domain source accompanying _develop_ 15.
-
- 07/31/94 zz add ColorQDIsPresent()
- 07/28/94 zz add offscreen drawing stuff
- **************************************************************************/
- #include "UGBDraw.h"
-
- #include <GestaltEqu.h>
-
- /**************************************************************************
- PenNormal()
-
- Restore the pen color to black on grety
- **************************************************************************/
- void
- UGBDraw::PenNormal(void)
- {
- ForeGrey(UGBDraw_black);
- BackGrey(UGBDraw_background);
- }
-
- /**************************************************************************
- PenReallyNormal()
-
- Restore the pen color to black on white
- **************************************************************************/
- void
- UGBDraw::PenReallyNormal(void)
- {
- ForeGrey(UGBDraw_black);
- BackGrey(UGBDraw_white);
- }
-
- /**************************************************************************
- ForeGrey()
-
- Set the pen to a grey level.
- **************************************************************************/
- void
- UGBDraw::ForeGrey(unsigned short inGrey)
- {
- RGBColor color;
- color.red = color.green = color.blue = inGrey;
- ::RGBForeColor(&color);
- }
-
-
- /**************************************************************************
- BackGrey()
-
- Set the pen to a grey level.
- **************************************************************************/
- void
- UGBDraw::BackGrey(unsigned short inGrey)
- {
- RGBColor color;
- color.red = color.green = color.blue = inGrey;
- ::RGBBackColor(&color);
- }
-
- /**************************************************************************
- OffscreenPre()
-
- Set up an offscreen GWorld for the current clip rect
- **************************************************************************/
- void
- UGBDraw::OffscreenPre(Offscreen &outOffscreen)
- {
- // clear out the gworld field in case we fail for some reason
- outOffscreen.gworld = NULL;
-
- // save the old settings
- ::GetGWorld(&outOffscreen.savePort,&outOffscreen.saveDevice);
-
- { // get the current clip rgn's bounds
- RgnHandle rgn = ::NewRgn();
- if (!rgn) return;
- ::GetClip(rgn);
- outOffscreen.lbounds = (**rgn).rgnBBox;
- ::DisposeRgn(rgn);
-
- // convert the bounds from local to global
- outOffscreen.gbounds = outOffscreen.lbounds;
- ::LocalToGlobal(&topLeft(outOffscreen.gbounds));
- ::LocalToGlobal(&botRight(outOffscreen.gbounds));
-
- }
-
- // create an offscreen GWorld in temporary memory with the
- // given bounds. We promise to release the temp memory
- // before the user notices its gone.
- OSErr err = ::NewGWorld(&outOffscreen.gworld,0,&outOffscreen.gbounds,
- NULL,NULL,useTempMem);
- if (err || !outOffscreen.gworld) {
- outOffscreen.gworld = NULL;
- return;
- }
-
- // switch drawing over to offworld city
- ::SetGWorld(outOffscreen.gworld,NULL);
-
- // make sure drawing code doesn't notice the difference
- ::SetOrigin(outOffscreen.lbounds.left,outOffscreen.lbounds.top);
-
- // ALWAYS LockPixels() before drawing. If you ever have problems
- // with your offscreen drawing code, checking LockPixels() is the
- // first thing you should check. At least that's been my experience...
- ::LockPixels(::GetGWorldPixMap(outOffscreen.gworld));
-
- // clean the slate
- ::EraseRect(&outOffscreen.lbounds);
- }
-
- /**************************************************************************
- OffscreenPost()
-
- Copy an offscreen GWorld back to the screen and nuke it
- **************************************************************************/
- void
- UGBDraw::OffscreenPost(Offscreen &ioOffscreen)
- {
- if (!ioOffscreen.gworld) return; // nothing to copy from, drawing
- // went directly to the screen
-
- // restore drawing to the screen
- ::SetGWorld(ioOffscreen.savePort,ioOffscreen.saveDevice);
-
- // copy the gworld to the screen. Oooh. Aaah.
- ::ForeColor(blackColor);
- ::BackColor(whiteColor);
- ::CopyBits(&((GrafPtr) ioOffscreen.gworld)->portBits,
- &((GrafPtr) ioOffscreen.savePort)->portBits,
- &ioOffscreen.lbounds,&ioOffscreen.lbounds,
- srcCopy,NULL);
-
- // release the gworld. I don't think I really need to unlock
- // the pixels before disposing it, but what can it hurt?
- ::UnlockPixels(::GetGWorldPixMap(ioOffscreen.gworld));
- ::DisposeGWorld(ioOffscreen.gworld);
- ioOffscreen.gworld = NULL;
- }
-
- /**************************************************************************
- ColorQDIsPresent()
- **************************************************************************/
- Boolean
- UGBDraw::ColorQDIsPresent(void)
- {
- Boolean present = false;
- long result;
- OSErr err = ::Gestalt(gestaltQuickdrawFeatures,&result);
- if (!err && ((result & (1L << gestaltHasColor)) != 0)) {
- present = true;
- }
- return present;
- }
-